package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 10:22 2021/10/8
 */

import com.liu.community.cache.TagCache;
import com.liu.community.dto.QuestionDto;
import com.liu.community.mapper.QuestionMapper;
import com.liu.community.pojor.Question;
import com.liu.community.pojor.User;
import com.liu.community.server.QuestionService;
import com.liu.community.server.UserService;
import com.liu.community.utils.URLUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PublishController {
    @Autowired
    private QuestionService questionService;

    @GetMapping("/publish")
    public  String goPublish(Model model){
        model.addAttribute("tags", TagCache.get());
        return  "publish";
    }
    @PostMapping("/publish")
    public  String doPublish(
            @RequestParam("title") String title,
            @RequestParam("description") String description,
            @RequestParam("tag") String tag ,
            HttpServletRequest request,
            Model model){
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());
        if (title == null || "".equals(title) ){
            model.addAttribute("error","标题不能为空！");
            return  "publish";
        }
        if (description == null || "".equals(description)){
            model.addAttribute("error","问题补充不能为空！");
            return  "publish";
        }
        if (tag == null || "".equals(tag)){
            model.addAttribute("error","标签不能为空！");
            return  "publish";
        }
        /*    String filerInvalid = TagCache.filerInvalid(tag);
        if (StringUtils.isNotBlank(filerInvalid)){
            model.addAttribute("error","输入非法标签！" +filerInvalid);
            return  "publish";
        }*/
        User user = (User) request.getSession().getAttribute("user");
        //用户没登录返回
        if (user == null){
            model.addAttribute("error","用户没有登录！");
            return  "publish";
        }
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        questionService.createOrUpdateQuestion(question);
        return URLUtils.sendRedirectTo("/index");
    }

    @PostMapping("/doUpdate")
    public  String doUpdatePublish(
            @RequestParam("title") String title,
            @RequestParam("description") String description,
            @RequestParam("tag") String tag ,
            HttpServletRequest request,
            Model model,
            @RequestParam("id")Integer id){
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());
        if (title == null || title == ""){
            model.addAttribute("error","标题不能为空！");
            return  "publish";
        }
        if (description == null || description == ""){
            model.addAttribute("error","问题补充不能为空！");
            return  "publish";
        }
        if (tag == null || tag == ""){
            model.addAttribute("error","标签不能为空！");
            return  "publish";
        }
    /*    String filerInvalid = TagCache.filerInvalid(tag);
        if (StringUtils.isNotBlank(filerInvalid)){
            model.addAttribute("error","输入非法标签！" +filerInvalid);
            return  "publish";
        }*/
        User user = (User) request.getSession().getAttribute("user");
        //用户没登录返回
        if (user == null){
            model.addAttribute("error","用户没有登录！");
            return  "publish";
        }
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        question.setId(id);
        questionService.createOrUpdateQuestion(question);
        return URLUtils.sendRedirectTo("/index");
    }

    @GetMapping("/publish/edit/{id}")
    public  String edit(@PathVariable(name = "id")Integer id,Model model){
        QuestionDto question = questionService.queryQuestionById(id);
        model.addAttribute("title",question.getTitle());
        model.addAttribute("description",question.getDescription());
        model.addAttribute("tag",question.getTag());
        model.addAttribute("id",question.getId());

        model.addAttribute("tags", TagCache.get());
        return "publishUpdate";
    }



}
